<?php
declare (strict_types=1);

namespace app\middleware;

use app\services\Cart;
use think\facade\Db;

class OrderCheck
{
    /**
     * 处理请求
     * @param \think\Request $request
     * @param \Closure $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        RedisLock('payShop_' . $request->user_id);
        try {
            if ($request->action() == 'payment') {
                if ($request->data['order_type'] != 3) {
                    return $next($request);
                }
            }
            $request->address = Db::name('address')->where(['id' => $request->data['address_id']])->find();
            if (empty($request->address)) {
                return app('json')->json_error('收货地址不存在');
            }
            if ($request->address['user_id'] != $request->user_id) {
                return app('json')->json_error('收货地址信息异常');
            }
            $request->order = json_decode($request->data['order'], true);
            if (empty($request->order)) {
                return app('json')->json_error('参数错误');
            }
            $spec_stock_msg = '';//验证库存
            $spec_msg = '';//验证规格
            $goods_msg = '';//验证商品
            //检查库存
            $order = $request->order;
            $order_type = $request->data['type'];
            foreach ($order as &$v) {
                if ($order_type == 2) {
                    $v['num'] = Cart::getInstance()->cartNum($request->user_id, $v['goods_spec_id']);
                }
                $spec = Db::name('commodity_spec')
                    ->field('a.price,a.spec_text,a.stock,b.name as goods_name,b.status as goods_status,b.grounding,b.condition')
                    ->alias('a')
                    ->leftJoin('commodity_goods b', 'a.goods_id = b.id')
                    ->where(['a.goods_spec_id' => $v['goods_spec_id']])
                    ->find();
                $v['price'] = $spec['price'];
                if ($v['num'] === false || $v['num'] < 0 || $v['num'] > $spec['stock']) {
                    $spec_stock_msg .= "{$spec['goods_name']} 规格： {$spec['spec_text']},";
                }
                if (empty($spec)) {
                    $spec_msg .= "{$spec['goods_name']} 规格：{$spec['spec_text']},";
                }
                if ($spec['goods_status'] != 1 || $spec['grounding'] != 1 || $spec['condition'] != 1) {
                    $spec_msg .= "{$spec['goods_name']} 规格： {$spec['spec_text']},";
                }
            }
            if ($spec_stock_msg != '') {
                return app('json')->json_error('商品：' . $spec_stock_msg . '库存不足。请清理后重新下单');
            }
            if ($spec_msg != '') {
                return app('json')->json_error('商品：' . $spec_msg . '商品规格不存在。请清理后重新下单');
            }
            if ($goods_msg != '') {
                return app('json')->json_error('商品：' . $goods_msg . '已被下架。请清理后重新下单');
            }
            if ($request->action() == 'payment') {
                return $next($request);
            } else {
                $request->data = ['address_id' => $request->data['address_id'], 'province' => $request->address['province'], 'order' => $order, 'type' => $order_type];
            }
            return $next($request);
        } catch (\Exception $exception) {
            return app('json')->json_error('下单异常' . $exception->getMessage());
        }
    }
}
